package lanou3g.myfirstapp36kr.activity;

import android.content.Intent;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.VolleyError;

import lanou3g.myfirstapp36kr.R;
import lanou3g.myfirstapp36kr.StringUrl;
import lanou3g.myfirstapp36kr.adapter.ActivityNearfutureActivityAdapter;
import lanou3g.myfirstapp36kr.base.BaseActivity;
import lanou3g.myfirstapp36kr.entity.ActivityNearfutureActivityEntity;
import lanou3g.myfirstapp36kr.myvolleysingleton.VolleySingleton;

/**
 * Created by dllo on 16/8/7.
 */
public class NearwFutureActivityActivity extends BaseActivity implements OnClickListener {
    private ImageView imageViewActType;
    private ImageView imageViewActTime;
    private ListView listView;
    private ActivityNearfutureActivityAdapter adapter;
    private PopupWindow popupWindow;
    private PopupWindow popupWindowTime;
    private ActivityNearfutureActivityEntity response;
    @Override
    protected int getLayout() {
        return R.layout.activity_find_nearfuture_activity;
    }

    @Override
    protected void initView() {
        imageViewActType = (ImageView) findViewById(R.id.iv_activity_type);
        imageViewActTime = (ImageView) findViewById(R.id.iv_activity_time);
        listView = (ListView) findViewById(R.id.list_view_nearfuture_activity);
        adapter = new ActivityNearfutureActivityAdapter(this);
        findViewById(R.id.iv_back).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }

    @Override
    protected void initData() {
        imageViewActType.setOnClickListener(this);
        imageViewActTime.setOnClickListener(this);
        ;
        VolleySingleton.addRequest(StringUrl.stringNewFuture, ActivityNearfutureActivityEntity.class,
                new Response.Listener<ActivityNearfutureActivityEntity>() {
                    @Override
                    public void onResponse(ActivityNearfutureActivityEntity response) {
                        adapter.setEntity(response);
                        NearwFutureActivityActivity.this.response = response;
                        listView.setAdapter(adapter);
                    }
                }, new ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.getMessage();
                    }
                });


        //ListView详情
        listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(NearwFutureActivityActivity.this,TurnPicDetailsActivity.class);
                String stringUrl = response.getData().getData().get(i).getActivityLink();
                intent.putExtra("urlNum",stringUrl);
                startActivity(intent);

            }
        });

    }
    public void request(String strUrl){
        VolleySingleton.addRequest(strUrl, ActivityNearfutureActivityEntity.class,
                new Response.Listener<ActivityNearfutureActivityEntity>() {
                    @Override
                    public void onResponse(ActivityNearfutureActivityEntity response) {
                        NearwFutureActivityActivity.this.response = response;
                        adapter.setEntity(response);

                        //listView.setAdapter(adapter);
                    }
                }, new ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.getMessage();

                    }
                });


    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.iv_activity_type:
                popupWindow = new PopupWindow(this);
                popupWindow.setWidth(900);
                popupWindow.setHeight(1000);
                View viewPop = LayoutInflater.from(this).inflate(R.layout.popwindow_nearfuture_activity,null);
                //初始化popWindow里的imageView
                viewPop.findViewById(R.id.tv_all).setOnClickListener(this);
                viewPop.findViewById(R.id.tv_demo_day).setOnClickListener(this);
                viewPop.findViewById(R.id.tv_room).setOnClickListener(this);
                viewPop.findViewById(R.id.tv_money).setOnClickListener(this);
                viewPop.findViewById(R.id.tv_Service).setOnClickListener(this);
                viewPop.findViewById(R.id.tv_quck).setOnClickListener(this);
                viewPop.setFocusable(true);
                viewPop.setFocusableInTouchMode(true);
                popupWindow.setContentView(viewPop);
                popupWindow.setFocusable(true);
                viewPop.setOnKeyListener(new OnKeyListener() {
                    @Override
                    public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
                        if (keyCode == KeyEvent.KEYCODE_BACK){
                            popupWindow.dismiss();
                        }
                        return false;
                    }
                });
                popupWindow.showAsDropDown(imageViewActType,20,0);
                break;
            case R.id.iv_activity_time:
                popupWindowTime = new PopupWindow(this);
                popupWindowTime.setWidth(900);
                popupWindowTime.setHeight(1000);
                View viewPopTime = LayoutInflater.from(this).inflate(R.layout.popwindow_nearfuture_activity_time,null);
                viewPopTime.setFocusable(true);
                viewPopTime.setFocusableInTouchMode(true);
                popupWindowTime.setFocusable(true);
                viewPopTime.setOnKeyListener(new OnKeyListener() {
                    @Override
                    public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
                        if (keyCode == KeyEvent.KEYCODE_BACK){
                            popupWindowTime.dismiss();
                        }
                        return false;
                    }
                });
                popupWindowTime.setContentView(viewPopTime);
                popupWindowTime.showAsDropDown(imageViewActTime,20,0);
                break;
            case R.id.tv_all:
                imageViewActType.setImageResource(R.drawable.all);
                popupWindow.dismiss();
                request("https://rong.36kr.com/api/mobi/activity/list?page=1");
                break;
            case R.id.tv_demo_day:
                imageViewActType.setImageResource(R.drawable.demoday);
                popupWindow.dismiss();
                request("https://rong.36kr.com/api/mobi/activity/list?page=1&categoryId=1&pageSize=20");
                break;
            case R.id.tv_room:
                imageViewActType.setImageResource(R.drawable.room);
                popupWindow.dismiss();
                request("https://rong.36kr.com/api/mobi/activity/list?page=1&categoryId=4&pageSize=20");
                break;
            case R.id.tv_money:
                imageViewActType.setImageResource(R.drawable.money_type);
                popupWindow.dismiss();
                request("https://rong.36kr.com/api/mobi/activity/list?page=1&categoryId=5&pageSize=20");
                break;
            case R.id.tv_Service:
                imageViewActType.setImageResource(R.drawable.service);
                popupWindow.dismiss();
                request("https://rong.36kr.com/api/mobi/activity/list?page=1&categoryId=6&pageSize=20");
                break;
            case R.id.tv_quck:
                popupWindow.dismiss();
                request("https://rong.36kr.com/api/mobi/activity/list?page=1&categoryId=7&pageSize=20");
                break;

        }


    }


}
